#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
RAG系统快速诊断
"""

import sys
import os
import json

# 添加当前目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

def test_rag_system():
    print("=" * 50)
    print("RAG系统快速诊断")
    print("=" * 50)
    
    try:
        # 测试简化版RAG系统
        print("1. 测试简化版RAG系统...")
        from simple_rag_system import SimpleRAGSystem
        rag = SimpleRAGSystem()
        print("   ✓ 简化版RAG系统初始化成功")
        
        # 测试问答
        print("2. 测试问答功能...")
        result = rag.get_answer("乳腺癌有哪些症状？")
        print(f"   ✓ 问答成功")
        print(f"   ✓ 置信度: {result['confidence']:.2f}")
        print(f"   ✓ 答案长度: {len(result['answer'])} 字符")
        print(f"   ✓ 答案预览: {result['answer'][:100]}...")
        
        # 测试搜索
        print("3. 测试搜索功能...")
        results = rag.search("症状", top_k=2)
        print(f"   ✓ 搜索成功，找到 {len(results)} 条结果")
        
        print("=" * 50)
        print("✓ RAG系统工作正常！")
        print("=" * 50)
        return True
        
    except Exception as e:
        print(f"✗ RAG系统测试失败: {e}")
        import traceback
        traceback.print_exc()
        return False

def test_flask_app():
    print("\n" + "=" * 50)
    print("Flask应用测试")
    print("=" * 50)
    
    try:
        from app_new import app, rag_system, RAG_AVAILABLE
        print(f"✓ Flask应用导入成功")
        print(f"✓ RAG_AVAILABLE: {RAG_AVAILABLE}")
        print(f"✓ rag_system: {'已初始化' if rag_system else '未初始化'}")
        
        if rag_system:
            # 测试RAG系统
            result = rag_system.get_answer("测试问题")
            print(f"✓ RAG系统测试成功，置信度: {result['confidence']:.2f}")
        else:
            print("✗ RAG系统未初始化")
            
        return True
        
    except Exception as e:
        print(f"✗ Flask应用测试失败: {e}")
        import traceback
        traceback.print_exc()
        return False

if __name__ == "__main__":
    print("Python版本:", sys.version)
    print()
    
    # 测试RAG系统
    rag_ok = test_rag_system()
    
    # 测试Flask应用
    flask_ok = test_flask_app()
    
    print("\n" + "=" * 50)
    if rag_ok and flask_ok:
        print("🎉 所有测试通过！RAG系统应该正常工作")
        print("如果网页仍显示'正在思考中'，请检查浏览器控制台错误")
    else:
        print("❌ 发现问题，请查看上面的错误信息")
    print("=" * 50)
    
    input("\n按回车键退出...")
